package sunwin.yoms.service.system;

import jodd.datetime.JDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.dao.FireWallDao;
import sunwin.yoms.domain.system.FireWallDomain;

import java.util.List;

/**
 * Created by watson on 2015/10/30.
 */
@Service
public class FireWallService {
    @Autowired
    private FireWallDao fireWallDao;

    /**
     * 查询防火墙信息
     */
    public List<FireWallDomain> getFireList(FireWallDomain fireWallDomain) throws YomsException {
        List<FireWallDomain> fireWallDomains = fireWallDao.getDomainCollection(fireWallDomain);
        if (fireWallDomains.size() <= 0) {
            throw new YomsException("未找到数据");
        } else {
            return fireWallDomains;
        }
    }

    /**
     * 增加防火墙信息
     */
    public void addFireWall(FireWallDomain fireWallDomain) throws YomsException {
        fireWallDomain.setFireCreateTime(new JDateTime(System.currentTimeMillis()).convertToDate());
        FireWallDomain Condition = new FireWallDomain();
        Condition.setFireIp(fireWallDomain.getFireIp());
        List<FireWallDomain> domains = fireWallDao.getDomainByCondition(Condition);
        if (domains.size() > 0) {
            throw new YomsException("该IP地址已存在");
        } else {
            fireWallDao.addDomain(fireWallDomain);
        }
    }

    /**
     * 编辑防火墙信息
     */
    public void editFireWall(FireWallDomain fireWallDomain) throws YomsException {
        FireWallDomain domain = fireWallDao.getDomainById(fireWallDomain.getFireId());
        if (domain == null) {
            throw new YomsException("非法请求");
        } else {
            fireWallDao.updateDomain(fireWallDomain);
        }
    }

    /**
     * 删除防火墙信息
     */
    public void deleteFire(int id) throws YomsException {
        FireWallDomain domain = fireWallDao.getDomainById(id);
        if (domain == null) {
            throw new YomsException("防火前不存在");
        } else {
            fireWallDao.deleteDomain(domain);
        }
    }

    /**
     * 根据ID查找防火墙
     */
    public FireWallDomain getFireWallById(int id)throws YomsException{
        FireWallDomain domain=fireWallDao.getDomainById(id);
        if (domain==null){
            throw new YomsException("该数据不存在");
        }
        else {
            return domain;
        }
    }
}
